--[[
Open Tibia Advanced Lib (Otal)
Made by: Mock
Tested in: TFS 0.3.(5,4,3,2,1)
Tested in: Open tibia SVN


All this functions was made by:
Mock, skyen hasus, nord, magus, dark billie,
colandus and shawak. And libs like luasocket
etc was made by other group of peoples.

Please report bugs on: http://otal.awardspace.info/
Contact: mock_otnet@hotmail.com
]]

--[[
 * Author: Mock
 * Version: 1.00.0
 * How to use:
 > local kind = 2 -- you can change to see another mode
 > local XML = [==[
 <?xml version="1.0" encoding="UTF-8"?>
 <flags>
    <flag summonable="0"/>
    <flag attackable="0"/>
    <flag hostile="1"/>
    <flag illusionable="0"/>
    <flag convinceable="0"/>
    <flag pushable="1"/>
    <flag canpushitems="1"/>
    <flag canpushcreatures="0"/>
    <flag targetdistance="1"/>
    <flag staticattack="90"/>
    <flag runonhealth="0"/>
  </flags>
 ]==]
 > local read = xml:open(XML) -- or xml:open('<xml string="..../>')
 > read:setSess('flags','flag','mysession') -- <flags>, <flag summonable="0"/>, name of this session?
 > print(type(read:getSess('mysession'))) --- Will print on console an swtring with XML code fo session flag: <flags><flag summonab...
 > if kind == 1 then
 >> table_ = read:readSess('mysession')
 > else
 >> table_ = read:readFree('flag')
 > end
 > print('-----')
 > for k,v in pairs(table_) do
 >> for m,n in pairs(v) do
 >>> print(m,n)
 >> end
 > end
 >>> print:
 table
 -----
 summonable, 0
 attackable, 0
 hostile, 1
 illusionable, 0
 convinceable, 0
 pushable, 1
 canpushitems, 1
 canpushcreatures, 0
 targetdistance, 1
 staticattack, 90
 runonhealth, 0
 
 
 * Special thanks for:
 ** Skyen hasus
 ** Magus
]]--

xml = {} -- classe
function xml:open(file,kind)
         if kind ~= nil then
            local file2 = assert(io.open(file,'r'))
            file = file2:read(-1)
            file2:close()
         end
         if file == nil then
            return error('File is empyt')
         end
         alt = string.explode(file,'\n',2)
         if alt[1] == nil or string.find(alt[1],"<?xml") == nil then
            return error('Bad xml format')
         end
         file = {text=alt[2],encoding=alt[1],session={}}
         setmetatable(file, {__index = xml})
         return file
end

function xml:setSess(sessname,tagname,name)
         xmlstr = self.text
         local f = ""
         for ret in xmlstr:gmatch('<'..sessname..'>(.-)</'..sessname..'>') do
             f = ret
         end
         if f:len() == 0 then
	        return error('Cannot find session called '..sessname..'.')
         end
         self.session[sessname] = {f,tagname=tagname,sessname=sessname}
         return TRUE
end

function xml:getSess(SessName)
   return self.session[SessName]
end

function xml:readSess(SessName)
local ret = self.session[SessName]
local t,x = {},1
for xmla in ret[1]:gmatch('<'..ret.tagname..' (.-)/>') do
        t[x] = {}
        for i, v in xmla:gmatch('([^ ]-)="(.-)"') do
            t[x][i] = v
        end
        x = x + 1
    end
    return t
end
function xml:readFree(tagname)
local ret = self.text
local t,x = {},1
for xmla in ret:gmatch('<'..tagname..' (.-)/>') do
        t[x] = {}
        for i, v in xmla:gmatch('([^ ]-)="(.-)"') do
            t[x][i] = v
        end
        x = x + 1
    end
    return t
end